//优惠券路由

// var express = require('express');
// var router = express.Router();
// const couponModel = require("../src/models/couponModel");
// /* GET users listing. */
//   router.get('/getcoupon', function(req, res, next) {
//     let a=couponModel.find().exec()
//     a.then(
//       (data)=>{
//        res.send(data);
//      })
//   });
// module.exports = router;


var express = require('express');
var router = express.Router();
const couponService = require('../src/services/couponService');

router.post('/',async function(req,res){
    let {name,coupons,func,date,intro} = req.body;
     let data = await couponService.insert({name, coupons, func,date,intro});
     res.send(data);
});

router.get('/',async function(req,res){
    console.log(req.query);
    let {type,value,current,pageSize} = req.query;
    let searchParams = {};
    if(type){
        if(type == "coupons"){
            searchParams[type] = value;
        }else{
            searchParams[type] = {$regex:value};
        }
    }
    let data = await couponService.find({current:current || 1,pageSize:pageSize || 5},searchParams);
    console.log(data);
    res.send(data);
});
router.get('/:id',async function(req,res){
    let {id} = req.params;
    let data = await couponService.findById(id);
    res.send(data);
});

router.delete('/:id',async function(req,res){
    let {id} = req.params;
    let data = await couponService.delete(id);
    res.send(data);
});

router.put('/:id',async function(req,res){
    let {id} = req.params;
    let {name,coupons,func,date,intro} = req.body;
    console.log({_id:id,name,coupons,func,date,intro})
    let data = await couponService.update({_id:id,name,coupons,func,date,intro});
    res.send(data);
});

module.exports = router;
